package com.ruoyi.web.controller.api;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysWord;
import com.ruoyi.system.domain.SysWordLearningProgress;
import com.ruoyi.system.mapper.SysWordMapper;
import com.ruoyi.system.service.ISysStudentCourseService;
import com.ruoyi.system.service.ISysWordLearningProgressService;

/**
 * 单词学习进度 控制层
 */
@RestController
@RequestMapping("/api/v1/word")
public class ApiWordController
{

    @Autowired
    private SysWordMapper sysWordMapper;

    @Value("${ruoyi.profile}")
    private String uploadPath;
    
    @Autowired
    private ISysStudentCourseService sysStudentCourseService;
    @Autowired
    private ISysWordLearningProgressService wordLearningProgressService;

    /**
     * 保存单词学习进度
     */
    @PostMapping("/learning/progress")
    public AjaxResult saveProgress(@RequestBody String body)
    {
    	SysWordLearningProgress progress = JSON.parseObject(body, SysWordLearningProgress.class);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        progress.setStudentId(loginUser.getStudentId());
        
        String currentWord = progress.getCurrentWord();
        SysWord sysWord = sysWordMapper.selectByWordName(currentWord);
        if (sysWord == null) {
			return AjaxResult.error("无效的单词，请联系客服！");
		}
        progress.setCurrentWordId(sysWord.getWordId());
        
        int rows = wordLearningProgressService.saveWordLearningProgress(progress);
        
        // 完成课程
        Long studentId = progress.getStudentId();
        Long courseId = progress.getCourseId();
        sysStudentCourseService.finishStudentCourse(studentId, courseId);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error("保存学习进度失败");
    }

    @GetMapping("/audio")
    public AjaxResult audio(@RequestParam(required = true) String word) {
        // 查询单词音频信息
        SysWord sysWord = sysWordMapper.selectByWordName(word);
        Map<String, Object> data = new HashMap<>();
        data.put("word", word);
        
        String wordAudioUrl = sysWord.getWordAudioUrl();
        if (wordAudioUrl == null || wordAudioUrl.length() == 0) {
            // 当没有音频数据时，返回成功状态但URL为空字符串
            data.put("audioUrl", "");
            return AjaxResult.success(data);
        }

        // 返回音频文件URL
        data.put("audioUrl", sysWord.getWordAudioUrl());
        return AjaxResult.success(data);
    }
}